package com.yc.springmvc.web;

import com.yc.springmvc.bean.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

// @RestController: 表示该类所有方法默认返回 json 格式数据
// ModelAndView: 实现页面跳转
@RestController
@RequestMapping("page/test")
public class HelloAction {

    @RequestMapping(path = "hello",
            method = RequestMethod.POST)
    public String hello(){
        return "hello world!";
    }

    @GetMapping("hello1")
    public String hello1(){
        return "hello china!";
    }

    @GetMapping(value="add", params = {"a","b"})
    public int add(int a, int b){
        return a + b;
    }

    @GetMapping(value="add", params = "a")
    public int add1(int a){
        return a + 100;
    }

    @GetMapping(value = "head", headers = "Host!=localhost:8080")
    public String headers(){
        return "Host";
    }

    @GetMapping(value = "head", headers = "Host=localhost:8080")
    public String headers1(){
        return "Host=localhost";
    }

    @PostMapping(value = "body" ,
            // 表单类型
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public String body(@RequestBody String body){
        return "<pre>" + body + "</pre>";
    }
    @PostMapping(value = "body" ,
            // 表单类型
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String body1(@RequestBody String body){
        return "<pre>" + body + "</pre>";
    }

    @GetMapping(path="hello2",
            // 相当于 response.setContentType("xxx");
            produces = MediaType.TEXT_HTML_VALUE+";charset=utf-8")
    public String hello2(String name){
        return "你好" + name;
    }

    @RequestMapping("toMypage")
    public ModelAndView toMypage(){
        ModelAndView mav = new ModelAndView();
        // 必须使用响应重定向, 才能跳转到 html
        mav.setViewName("redirect:/mypage.html");
        return mav;
    }

    @RequestMapping(path="user",
            produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    public User user(User user){
        return user;
    }

    @GetMapping(path = "servlet", produces = MediaType.TEXT_HTML_VALUE+";charset=utf-8")
    public String servlet(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session,
            InputStream in,
            OutputStream out
    ){
        List<Object> list = new ArrayList<>();
        list.add(request);
        list.add(response);
        list.add(session);
        list.add(in);
        list.add(out);
        String ret = "<ul>";
        for (Object o : list) {
            ret += "<li>" + o + "</li>";
        }
        ret +="</ul>";
        return ret;
    }


    // 复杂对象的提交
























}
